/**
 * Created by cjd on 2016/6/9.
 */
angular.module("app").controller("resetPassword.Ctrl", ["$scope", "$interval", "User", function ($scope, $interval, User) {
    $scope.resetPassword = function () {
        User.resetPassword({}, {
            password: $scope.password,
            code: $scope.checkCode,
            phone: $scope.phone
        }, function (data) {
            if (data.statusCode == 0) {
                alert("密码重置成功,请重新登录!");
                sessionStorage.clear();
                location.href = "#/login";
            } else {
                alert("重置密码出错,请重试!")
            }
        })
    };
    $scope.sendSMS = function () {
        if (!$scope.phone || $scope.phone.length == 0) {
            alert("手机号不能为空!");
            return;
        }
        if($scope.cd != "获取验证码"){
            return;
        }
        User.sendSMS({}, {phone: $scope.phone}, function (data) {
            if (data.statusCode == 0) {
                $scope.count = 60;
                $scope.timer = $interval(function () {
                    if ($scope.count == 0) {
                        $scope.cd = "获取验证码";
                        $interval.cancel($scope.timer);
                    }
                    else {
                        $scope.count--;
                        $scope.cd = "剩余" + $scope.count + "秒";
                    }
                }, 1000)
            }
            else if (data.statusCode == -2) {
                alert("该手机号不存在,请重新输入!")
            }
            else {
                alert(data.statusMessage)
            }
        })
    };
    $scope.cd = "获取验证码";
}]);